package com.ywb.ex03;
import java.awt.*;
import java.util.concurrent.TimeUnit;
import javax.swing.*;

/**
 * @author ywb
 */
public class Task3 extends JFrame {
    private Thread threadA; // 定义两个线程
    private Thread threadB;
    final JProgressBar progressBar = new JProgressBar(); // 定义两个进度条组件
    final JProgressBar progressBar2 = new JProgressBar();
    int count = 0;

    public static void main(String[] args) {
        init(new Task3(), 400, 100);
    }

    public Task3() {
        setTitle("进度条");
        setLocationRelativeTo(null);
        setResizable(false);
        // 将进度条设置在窗体最北面
        getContentPane().add(progressBar, BorderLayout.NORTH);
        // 将进度条设置在窗体最南面
        getContentPane().add(progressBar2, BorderLayout.SOUTH);
        progressBar.setStringPainted(true); // 设置进度条显示数字字符
        progressBar2.setStringPainted(true);
        // 使用匿名内部类形式初始化Thread实例子
        threadA = new Thread(new Runnable() {
            int count = 0;
            @Override
            public void run() {
                while(true){
                    progressBar.setValue(++count);
                    try{
                        TimeUnit.MILLISECONDS.sleep(100);
                        threadB.join();
                    }catch (InterruptedException ie){
                        ie.printStackTrace();
                    }
                    if(count == 100){
                        break;
                    }
                }
            }
        });
        threadA.start();
        threadB = new Thread(new Runnable() {
            int count = 0;
            @Override
            public void run() {
                while(true){
                    progressBar2.setValue(++count);
                    try{
                        TimeUnit.MILLISECONDS.sleep(100);
                    }catch (InterruptedException ie){
                        ie.printStackTrace();
                    }
                    if(count == 100){
                        break;
                    }
                }
            }
        });
        threadB.start();


    }

    // 设置窗体各种属性方法
    public static void init(JFrame frame, int width, int height) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}
